package com.heima.wemedia.service.impl;

import com.heima.feign.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.TaskDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmAutoScanService;
import com.heima.wemedia.service.WmTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author jack
 * @data 2024 16:17
 */
@Service
@Slf4j
public class WmTaskServiceImpl implements WmTaskService {
    @Autowired
    private IScheduleClient scheduleClient;
    @Autowired
    private WmAutoScanService wmAutoScanService;

    /**
     * 添加任务
     *
     * @param wmNews
     */
    @Async
    public void addTask(WmNews wmNews) {
        //封装任务数据： taskDto
        TaskDto taskDto = new TaskDto();
        taskDto.setTaskType(1001);
        taskDto.setPriority(1);
        taskDto.setParameters(String.valueOf(wmNews.getId()).getBytes());
        taskDto.setExecuteTime(wmNews.getPublishTime().getTime());

        //远程调用schedule微服务，添加任务
        scheduleClient.addTask(taskDto);
    }


    /**
     * 定时拉取审核文章任务，进行文章审核操作
     * 执行频次：每秒执行一次
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void pullTask(){
        ResponseResult responseResult = scheduleClient.pullTask(1001, 1);
        Integer code = responseResult.getCode();
        Object data = responseResult.getData();
        if (code == 200){
            log.info("===============定时拉取审核文章任务执行了，本次处理的文章id:{}==============",data);
            Integer newsId = Integer.valueOf(data.toString());
            wmAutoScanService.autoScan(newsId);
        }
    }
}
